package cn.yitun.chat.yitunserverimapicore.business.account.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.action.MethodMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserAccountDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.account.service.AccountService;
import cn.yitun.chat.yitunserverimapicore.business.user.service.SecurityQuestionService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-20 08:38:14<br>
 * Description 账号相关业务处理
 * 
 * @author yitun
 * @since 1.1
 */
@Component
@ActionMapping(value = ActionTypeConstant.ACCOUNT_ACTION_PUSH)
public class AccountAction {

	@Resource
	private SecurityQuestionService securityQuestionService;
	@Resource
	private AccountService accountService;

	/**
	 * Description 获取密保问题 <br>
	 * Date 2019-04-20 17:33:25<br>
	 * @param account
	 * @return
	 * @since 1.1
	 */
	@MethodMapping(value = "", isIntercept = false)
	@ActionMapping(value = "1.1.0001")
	public Object getSecurityQuestionList(@JsonDefine("data") UserAccountDto account) {

		return securityQuestionService.getSecurityQuestionDataListByAccount(account.getAccount());
	}

	/**
	 * Description <br>
	 * Date 2019-04-27 09:05:50<br>
	 * @param accountDto
	 * @return Object
	 * @since 1.1
	 */
	@MethodMapping(value = "", isIntercept = false)
	@ActionMapping(value = "1.1.0002")
	public Object updatePassword(
			@JsonDefine("data") UserAccountDto accountDto) {
		if (NumUtil.isInValid(accountDto.getUserId()) || YitunStringUtil.isEmpty(accountDto.getPassword()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		ResultMessage message = new ResultMessage();
		message.setInfo(securityQuestionService.updatePassword(accountDto));
		return message;
	}

	/**
	 * Description 账号是否存在 <br>
	 * Date 2019-04-28 00:23:46<br>
	 * @param accountDto
	 * @return Object
	 * @since 1.1
	 */
	@MethodMapping(value = "", isIntercept = false)
	@ActionMapping(value = "1.1.0005")
	public Object isExistAccount(
			SocketSession socketSession,
			@JsonDefine("data") UserAccountDto accountDto) {
		if (NumUtil.isInValid(accountDto.getUserId()) || YitunStringUtil.isEmpty(accountDto.getAccount()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long userId = accountDto.getUserId();
		String account = accountDto.getAccount();
//		userId = socketSession.getKey();
		boolean exist = accountService.isExistAccount(userId, account);
		return new ResultMessage(R.param("exist", exist));
	}

	/**
	 * Description 邮箱是否已经存在 <br>
	 * Date 2019-04-28 00:27:49<br>
	 * @param accountDto
	 * @return Object
	 * @since 1.1
	 */
	@MethodMapping(value = "", isIntercept = false)
	@ActionMapping(value = "1.1.0006")
	public Object isExistEmail(
			SocketSession socketSession,
			@JsonDefine("data") UserAccountDto accountDto) {
		if (NumUtil.isInValid(accountDto.getUserId()) || YitunStringUtil.isEmpty(accountDto.getEmail()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);
//		userId = socketSession.getKey() ;
		boolean exist = accountService.isExistEmail(accountDto.getUserId(), accountDto.getEmail());
		return new ResultMessage(R.param("exist", exist));
	}

	/**
	 * Description 手机号码是否存在 <br>
	 * Date 2019-04-28 00:29:11<br>
	 * @param accountDto
	 * @return Object
	 * @since 1.0.0
	 */
	@MethodMapping(value = "", isIntercept = false)
	@ActionMapping(value = "1.1.0007")
	public Object isExistMobile(
			SocketSession socketSession,
			@JsonDefine("data") UserAccountDto accountDto) {
		if (NumUtil.isInValid(accountDto.getUserId()) || YitunStringUtil.isEmpty(accountDto.getMobile()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);
//		userId = socketSession.getKey();
		boolean exist = accountService.isExistMobile(accountDto.getUserId(), accountDto.getMobile());
		return new ResultMessage(R.param("exist", exist));
	}
}
